package com.bafa.front;

import com.bafa.dbcache.LeagueCache;
import com.bafa.debug.Debug;
import com.bafa.exceptions.ErrorException;
import com.bafa.front.events.Event;
import com.bafa.front.utils.FrontUtils;

import com.bafa.objects.Message;
import com.bafa.objects.PageBean;
import com.bafa.services.RetrieveServices;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MasterServlet extends DaddyServlet {
    public void doEvent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ErrorException {
        String page = request.getParameter("page");
        if(page==null || page.equals(""))
			page = "home";
        PageBean pageBeanTemp = RetrieveServices.getPage(page);
        pageBean = new PageBean(pageBeanTemp);
		Message message = new Message();
        try{
			
			if(pageBean.isPostlogon()&&!FrontUtils.isLoggedOn(request)){
				pageBean = RetrieveServices.getPage("logon");
			}
			else if(pageBean.isAdmin()&&!FrontUtils.isLoggedOnAsAdmin(request))
				pageBean = RetrieveServices.getPage("logon");
			else{
				Class c = Class.forName("com.bafa.front.events."+pageBean.getEvent());
				Event event = (Event)c.newInstance();
				event.doEvent(request,response,pageBean);
				request.setAttribute("MESSAGE",event.message);
			}
			
        }catch(Exception e){
			Debug.printStackTrace(e);
        }
    
    }
}
